/**
 * @program: clms
 * @description:
 * @author: mitu_echo
 * @create: 2021-07-16 15:21
 **/

package com.second.system.shiro;


import com.second.system.pojo.entity.User;
import com.second.system.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {


        String username = authenticationToken.getPrincipal().toString();
        User user = userService.queryUserByName(username);
        if (user != null) {
            ByteSource byteSource = ByteSource.Util.bytes(user.getSalt());
            return new SimpleAuthenticationInfo(user,user.getUserpwd(),byteSource,this.getName());
        }
        return null;
    }
}
